﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Validation.Validators
{
	using System;
	using System.Globalization;

	public class BetweenValidator : AndStackedValidator
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="BetweenValidator"/> class.
		/// </summary>
		public BetweenValidator()
		{
			Validators = new IValidator[]
			{
				new EqualOrGreaterThanValidator(),
				new EqualOrLessThanValidator(),
			};
		}

		/// <summary>
		/// Validates the specified value.
		/// </summary>
		/// <param name="value">The value.</param>
		/// <param name="against">The against.</param>
		/// <returns></returns>
		public override bool Validate(object value, object against)
		{
			if (!(against is string))
			{
				return false;
			}

			// convert the value to something we can compare against
			var left = Convert.ToDouble(value, CultureInfo.InvariantCulture);

			// get values to validate against
			var range = against.ToString().Split(',');

			// values to validate against
			var right = new object[]
			{
				Convert.ToDouble(range[0], CultureInfo.InvariantCulture), 
				Convert.ToDouble(range[1], CultureInfo.InvariantCulture)
			};

			return ValidateArray(left, right);
		}
	}
}